﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sunboxsoft.WinForm
{
    public class ViewStack
    {
        private List<ViewStackNode> _list = new List<ViewStackNode>();

        public int Count
        {
            get
            {
                return _list.Count;
            }
        }

        public bool HasAny()
        {
            if (_list.Count > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 是否在当前视图后面还存在别的视图
        /// </summary>
        /// <returns></returns>
        public bool HasAnyBackground()
        {
            if (_list.Count > 0)
            {
                return true;
            }
            return false;
        }

        public void Push(ViewStackNode node)
        {
            _list.Add(node);
        }

        public ViewStackNode Top()
        {
            return _list.LastOrDefault();
        }

        public ViewStackNode Pop()
        {
            if (_list.Count > 0)
            {
                ViewStackNode node = _list[_list.Count - 1];
                _list.RemoveAt(_list.Count - 1);
                return node;
            }
            return null;
        }

        public void Clear()
        {
            _list.Clear();
        }

        /// <summary>
        /// 通过视图对象获取视图节点对象
        /// </summary>
        /// <param name="view"></param>
        /// <returns></returns>
        public ViewStackNode Get(IView view)
        {
            for (int index = 0; index < _list.Count; index++)
            {
                if (_list[index].View == view)
                {
                    return _list[index];
                }
            }
            return null;
        }

        public ViewStackNode GetAt(int index)
        {
            return _list[index];
        }
    }
}
